package com.egg.basic.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.egg.basic.service.secure.SecureService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.redis.util.RedisUtil;
import com.egg.common.util.SecureUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author east
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "加解密")
@RestController
@RequestMapping("/secure")
public class WebSecureController extends WebController {


    @Autowired
    private SecureService secureService;


    @ApiOperation(value = "获取非对称公钥")
    @GetMapping("/rsa/public/let")
    public ResponseData<String> secureRasPublic() {
        HashMap<String, String> pairHashMap = SecureUtil.rsaGenerateKeyPair();
        String publicKey = SecureUtil.rsaPublicKey(pairHashMap);
        String privateKey = SecureUtil.rsaPrivateKey(pairHashMap);
        // 公私钥有效期五分钟
        RedisUtil.set(secureService.buildSecureHeaderRedisKey(publicKey), privateKey, 300);
        return ResponseData.success(publicKey);
    }

    @ApiOperation(value = "公钥加密")
    @GetMapping("/rsa/encrypt/let")
    public ResponseData<String> secureRsaEncrypt(
            @ApiParam(value = "公钥", required = true) @RequestParam(value = "publicKey") String publicKey
            , @ApiParam(value = "需要加密的明文", required = true) @RequestParam(value = "content") String content
    ) {
        String aesDecryptContent = SecureUtil.rsaEncryptByPublic(publicKey, content);
        return ResponseData.success(aesDecryptContent);
    }

    @ApiOperation(value = "获取对称秘钥")
    @GetMapping("/rsa/aes/let")
    public ResponseData<Boolean> secureRsaAes(
            @ApiParam(value = "公钥", required = true) @RequestParam(value = "publicKey") String publicKey
            , @ApiParam(value = "公钥加密后的对称秘钥", required = true) @RequestParam(value = "aes") String aes
            , @ApiParam(value = "有效期", required = true) @RequestParam(value = "time") Integer time
    ) {
        String redisKey = secureService.buildSecureHeaderRedisKey(publicKey);
        String privateKey = RedisUtil.get(redisKey);
        if (ObjectUtil.isEmpty(privateKey)) {
            return ResponseData.error("获取传输秘钥失败");
        }
        String aesDecrypt = SecureUtil.rsaDecryptByPrivate(privateKey, aes);
        // 将有效期延迟5分钟
        time += 1000 * 60 * 5;
        RedisUtil.set(redisKey, aesDecrypt, time);
        return ResponseData.success(true);
    }

    @ApiOperation(value = "对称加密")
    @GetMapping("/aes/encrypt/let")
    public ResponseData<String> secureAesEncrypt(
            @ApiParam(value = "公钥") @RequestParam(value = "publicKey", required = false) String publicKey
            , @ApiParam(value = "对称秘钥") @RequestParam(value = "aes", required = false) String aes
            , @ApiParam(value = "需要加密的明文", required = true) @RequestParam(value = "content") String content
    ) {
        if (ObjectUtil.isEmpty(publicKey) && ObjectUtil.isEmpty(aes)) {
            return ResponseData.error("加密秘钥至少填写一个");
        }
        if (ObjectUtil.isEmpty(aes)) {
            aes = RedisUtil.get(secureService.buildSecureHeaderRedisKey(publicKey));
        }
        if (ObjectUtil.isEmpty(aes)) {
            return ResponseData.error("加密秘钥获取失败");
        }
        String aesDecryptContent = SecureUtil.aesEncrypt(aes, content);
        return ResponseData.success(aesDecryptContent);
    }

    @ApiOperation(value = "对称解密")
    @GetMapping("/aes/decrypt/let")
    public ResponseData<String> secureAesDecrypt(
            @ApiParam(value = "公钥") @RequestParam(value = "publicKey", required = false) String publicKey
            , @ApiParam(value = "对称秘钥") @RequestParam(value = "aes", required = false) String aes
            , @ApiParam(value = "需要解密的密文", required = true) @RequestParam(value = "content") String content
    ) {
        if (ObjectUtil.isEmpty(publicKey) && ObjectUtil.isEmpty(aes)) {
            return ResponseData.error("解密秘钥至少填写一个");
        }
        if (ObjectUtil.isEmpty(aes)) {
            aes = RedisUtil.get(secureService.buildSecureHeaderRedisKey(publicKey));
        }
        if (ObjectUtil.isEmpty(aes)) {
            return ResponseData.error("解密秘钥获取失败");
        }
        String aesDecryptContent = SecureUtil.aesDecrypt(aes, content);
        return ResponseData.success(aesDecryptContent);
    }


}
